package com.wushi.lenovo.asia5b.app_utils.eye;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.InputType;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudinary.Cloudinary;
import com.cloudinary.android.MediaManager;
import com.cloudinary.utils.ObjectUtils;
import com.mvp.base.BaseAppCompatActivity;
import com.mvp.helper.WCallBack;
import com.mvp.networking.ApiErrorHelper;
import com.mvp.utils.StringUtils;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_model.other.WebUrlType;
import com.wushi.lenovo.asia5b.app_ui.h5.CategoryActivity;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

import static timber.log.Timber.d;

/**
 * Created by zhangyuncai on 2019/3/13.
 * 这个是设置标签,看后期是否用得上
 */
public class EyeUtils {

    public static String KG = "KG";

    public static void tagPermission() {//方便开发

    }

    /**
     * 上传云图片
     *
     * @param wCallBack
     * @param activity
     */
    public static void uploadUrl(BaseAppCompatActivity activity, String path, WCallBack<String> wCallBack) {
        Cloudinary cloudinary = MediaManager.get().getCloudinary();
        Disposable subscribe = Observable.just(path)
                .map(new Function<String, Map>() {
                    @Override
                    public Map apply(String s) throws Exception {
                        //{signature=04cae05e7d6b79e02feb3ad79a6e8e35d28b2ca9,
                        // format=png,
                        // resource_type=image,
                        // secure_url=https://res.cloudinary.com/wushiyi/image/upload/v1552356224/haha.png,
                        // created_at=2019-03-12T02:03:44Z,
                        // type=upload,
                        // version=1552356224,
                        // url=http://res.cloudinary.com/wushiyi/image/upload/v1552356224/haha.png,
                        // public_id=haha,
                        // tags=[],
                        // original_filename=Screenshot_2019-03-11-11-22-53-343_com.wushi.aisa5bnew1,
                        // bytes=227809,
                        // width=1080,
                        // etag=08575e50190c176b9afb20b32fdb5dbe,
                        // placeholder=false,
                        // height=2160}
                        Map map = cloudinary.uploader().upload(s, ObjectUtils.emptyMap());
                        return map;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(disposable -> {
                    if (activity != null) {
                        activity.showLoadingBar(null);
                    }
                })
                .doAfterTerminate(() -> {
                    if (activity != null) {
                        activity.dismissLoadingBar("");
                    }
                })
                .subscribe(map -> {
                    d("--->>>>>>>>---map:" + map.toString());
                    String uploadUrl = "";
                    if (map != null) {
                        uploadUrl = map.get("url").toString();
                    }
                    if (wCallBack != null) {
                        wCallBack.callback(uploadUrl);
                    }
                }, throwable -> {
                    ApiErrorHelper.handleCommonError(throwable);
                    if (wCallBack != null) {
                        wCallBack.callback(null);
                    }
                });
        if (activity != null) {
            activity.composite.add(subscribe);
        }
    }

    /**
     * 文本设置超链接
     *
     * @param context
     * @param content
     * @param linkText
     * @param textView
     * @param webUrlType
     */
    public static void setLinkText(Context context, String content, String linkText, TextView textView, WebUrlType webUrlType) {
        int localColor = ContextCompat.getColor(context, R.color.colorPrimaryDark);
        SpannableString termsofuseSpannableString = StringUtils.getSpannableString(content, linkText, localColor
                , v -> {
                    if (webUrlType != null) {
                        CategoryActivity.startCategoryActivity(context, webUrlType);
                    }
                });
        textView.setText(termsofuseSpannableString);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
    }

    public static void bindEye(ImageView eyeView, EditText targetView) {
        eyeView.setOnClickListener(v -> {
            eyeView.setSelected(!eyeView.isSelected());
            if (eyeView.isSelected()) {
                eyeView.setImageResource(R.drawable.ic_eye);
                targetView.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            } else {
                eyeView.setImageResource(R.drawable.ic_noeye);
                targetView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            }
        });
    }

    public static void bindEyes(ImageView[] eyeViews, EditText[] targetViews) {
        View.OnClickListener v = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setSelected(!v.isSelected());

                if (v.isSelected()) {

                    for (ImageView eyeView : eyeViews) {
                        eyeView.setImageResource(R.drawable.ic_eye);
                    }
                    for (EditText targetView : targetViews) {
                        targetView.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    }

                } else {
                    for (ImageView eyeView : eyeViews) {
                        eyeView.setImageResource(R.drawable.ic_noeye);
                    }
                    for (EditText targetView : targetViews) {
                        targetView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    }

                }
            }
        };
        for (ImageView eyeView : eyeViews) {
            eyeView.setOnClickListener(v);
        }
    }

    public static void setNums(TextView textView, int num) {
        textView.setVisibility(num <= 0 ? View.INVISIBLE : View.VISIBLE);
        textView.setText(num > 99 ? "99+" : (num + ""));
    }
    public static void setNumsByGone(TextView textView, int num) {
        textView.setVisibility(num <= 0 ? View.GONE : View.VISIBLE);
        textView.setText(num > 99 ? "99+" : (num + ""));
    }
}
